﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;

using LRSTaxServices.DAL;
using LRSTaxServices.DAL.dsLRSTransactionsTableAdapters;
using LRSTaxServices.Common;

namespace LRSTaxServices.Biz
{
    /// <summary>
    /// LRSTransactions Business Object
    /// </summary>

    public class LRSTransactions : BOBase
    {
        #region Construction / Disposal

        /// <summary>
        /// Default constructor.
        /// </summary>
        protected LRSTransactions() { }

        #endregion

        #region Public Properties

        #region Instance Properties


        /// <summary>
        /// TransactionID.
        /// </summary>
        public int TransactionID
        {
            get { return _transactionID; }
            set
            {
                if (_transactionID != value)
                {
                    if (OnPropertyChanging("TransactionID", _transactionID, value))
                    {
                        int oldValue = _transactionID;
                        _transactionID = value;
                        OnPropertyChanged("TransactionID", oldValue, value);
                    }
                }
            }
        }  private int _transactionID = -1;


        /// <summary>
        /// Date/time at which the LRSTransactions was created.
        /// </summary>
        public DateTime? TransactionDate
        {
            get { return _transactionDate; }
            set
            {
                if (_transactionDate != value)
                {
                    if (OnPropertyChanging("TransactionDate", _transactionDate, value))
                    {
                        DateTime? oldValue = _transactionDate;
                        _transactionDate = value;
                        OnPropertyChanged("TransactionDate", oldValue, value);
                    }
                }
            }
        }  private DateTime? _transactionDate = null;



        /// <summary>
        /// UserID.
        /// </summary>
        public string UserID
        {
            get { return _userid; }
            set
            {
                if (_userid != value)
                {
                    if (OnPropertyChanging("UserID", _userid, value))
                    {
                        string oldValue = _userid;
                        _userid = value;
                        OnPropertyChanged("UserID", oldValue, value);
                    }
                }
            }
        }  private string _userid = null;
        



        /// <summary>
        /// RefID.
        /// </summary>
        public string RefID
        {
            get { return _refID; }
            set
            {
                if (_refID != value)
                {
                    if (OnPropertyChanging("RefID", _refID, value))
                    {
                        string oldValue = _refID;
                        _refID = value;
                        OnPropertyChanged("RefID", oldValue, value);
                    }
                }
            }
        }  private string _refID = null;


        /// <summary>
        /// TransactionType.
        /// </summary>
        public string TransactionType
        {
            get { return _transactionType; }
            set
            {
                if (_transactionType != value)
                {
                    if (OnPropertyChanging("TransactionType", _transactionType, value))
                    {
                        string oldValue = _transactionType;
                        _transactionType = value;
                        OnPropertyChanged("TransactionType", oldValue, value);
                    }
                }
            }
        }  private string _transactionType = null;


        /// <summary>
        /// Description.
        /// </summary>
        public string Description
        {
            get { return _description; }
            set
            {
                if (_description != value)
                {
                    if (OnPropertyChanging("Description", _description, value))
                    {
                        string oldValue = _description;
                        _description = value;
                        OnPropertyChanged("Description", oldValue, value);
                    }
                }
            }
        }  private string _description = null;


        /// <summary>
        /// Amount.
        /// </summary>
        public double Amount
        {
            get { return _amount; }
            set
            {
                if (_amount != value)
                {
                    if (OnPropertyChanging("Amount", _amount, value))
                    {
                        double oldValue = _amount;
                        _amount = value;
                        OnPropertyChanged("Amount", oldValue, value);
                    }
                }
            }
        }  private double _amount = 0;





        /// <summary>
        /// Balance.
        /// </summary>
        public double Balance
        {
            get { return _balance; }
            set
            {
                if (_balance != value)
                {
                    if (OnPropertyChanging("Balance", _balance, value))
                    {
                        double oldValue = _balance;
                        _balance = value;
                        OnPropertyChanged("Balance", oldValue, value);
                    }
                }
            }
        }  private double _balance = 0;

        #endregion

        #endregion

        #region Public Methods

        #region CreateNew

        /// <summary>
        /// Creates a new, empty LRSTransactions object.
        /// </summary>
        public static LRSTransactions CreateNew()
        {
            LRSTransactions obj = new LRSTransactions();

            // Switch state to Added prior to initializing property values.
            obj.ObjState = ObjectState.Added;

            obj.TransactionDate = DateTime.Now;
            return (obj);
        }


        [MethodImpl(MethodImplOptions.Synchronized)]
        public static LRSTransactions CreateNew(LRSTransactionsCollection collection)
        {
            LRSTransactions obj = CreateNew();
            collection.Add(obj);
            return (obj);
        }

        #endregion        

        #region GetAllTransactions

        /// <summary>
        /// Returns a collection of LRSTransactions.
        /// </summary>
        public static LRSTransactionsCollection GetAllTransactions()
        {
            LRSTransactionsTableAdapter adapter = new LRSTransactionsTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsLRSTransactions.LRSTransactionsDataTable tbl = adapter.GetData(null);

            LRSTransactionsCollection list = new LRSTransactionsCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsLRSTransactions.LRSTransactionsRow row in tbl.Rows)
                {
                    LRSTransactions LRSTransactions = new LRSTransactions();
                    RelationalToObject(LRSTransactions, row);
                    list.Add(LRSTransactions);
                }
            }

            return (list);
        }

        #endregion

        #region GetAllTransactionsByUserIDnTransactionType

        /// <summary>
        /// Returns a collection of LRSTransactions.
        /// </summary>
        public static LRSTransactionsCollection GetAllTransactionsByUserIDnTransactionType(string userid, string transactiontype)
        {
            LRSTransactionsTableAdapter adapter = new LRSTransactionsTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsLRSTransactions.LRSTransactionsDataTable tbl = adapter.GetDataByUserIDnTransactionType(userid,transactiontype);

            LRSTransactionsCollection list = new LRSTransactionsCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsLRSTransactions.LRSTransactionsRow row in tbl.Rows)
                {
                    LRSTransactions LRSTransactions = new LRSTransactions();
                    RelationalToObject(LRSTransactions, row);
                    list.Add(LRSTransactions);
                }
            }

            return (list);
        }

        #endregion

        #region Update

        /// <summary>
        /// Updates the specified LRSTransactions.
        /// </summary>
        public static void Update(LRSTransactions obj)
        {
            bool updateTransactions = false;

            // Do not update invalid objects.
            if (!obj.Validate())
            {
                StringBuilder sb = new StringBuilder(2048);
                sb.Append("The object being updated has one or more invalid properties; the Update() operation for this object has been cancelled.\nError(s):");
                foreach (ValidationError err in obj.Errors)
                    sb.Append("\n* " + err.Element + ": " + err.Message);

                throw new InvalidObjectException(sb.ToString());
            }
            else
            {
                updateTransactions = (obj.ObjState != ObjectState.Unchanged);

                if (!updateTransactions)
                    return;
            }

            ObjectState originalObjectState = obj.ObjState;

            dsLRSTransactions.LRSTransactionsDataTable tbl = null;
            if (updateTransactions)
            {
                LRSTransactionsTableAdapter adapter = new LRSTransactionsTableAdapter();
                adapter.Connection = AppSettings.GetDbConn();


                tbl = new dsLRSTransactions.LRSTransactionsDataTable();
                dsLRSTransactions.LRSTransactionsRow row = tbl.NewLRSTransactionsRow();
                ObjectToRelational(obj, tbl, row);

                adapter.Update(tbl);

                if (originalObjectState == ObjectState.Deleted)
                    obj = null;
                else RelationalToObject(obj, tbl.Rows[0]);
            }
        }

        #endregion

        #region Validate

        /// <summary>
        /// Validates the object based on its properties at the time of the call; updates the Errors property.
        /// </summary>
        public override bool Validate()
        {
            Errors.Clear();

            if (ObjState == ObjectState.Unchanged)
            {
                // By definition, unchanged objects are valid.
                return (true);
            }
            else if (ObjState == ObjectState.Uninitialized)
            {
                Errors.Add(new ValidationError("object", "Object is uninitialized.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else if ((ObjState == ObjectState.Deleted))
            {
                // Deleted items must have 'real' (i.e. non-negative, non-zero) key(s) to be valid.
                Errors.Add(new ValidationError("TransactionID", "Deleted object has an invalid PKID.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else  // ObjState == Added | Modified
            {

            }

            return (Errors.Count == 0);
        }

        #endregion

        #region ToString

        /// <summary>
        /// User-friendly string represetation of this object.
        /// </summary>
        public override string ToString()
        {
            return (String.Format("LRSTransactions: TransactionID={0}", TransactionID));
        }

        #endregion

        #endregion

        #region Privates


        #endregion

    }

    /// <summary>
    /// Collection of 0 or more SubAdmin objects.
    /// </summary>

    public class LRSTransactionsCollection : List<LRSTransactions>
    {
        #region Types

        public class UserComparer : IComparer<LRSTransactions>
        {
            public int Compare(LRSTransactions t1, LRSTransactions t2)
            {
                return (String.Compare(t1.TransactionID.ToString(), t2.TransactionID.ToString()));
            }
        }

        #endregion

        #region Public Methods

        #region FindByTransactionID

        public LRSTransactions FindByTransactionID(int transactionid)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].TransactionID == transactionid)
                    return (this[ndx]);

            return (null);
        }

        #endregion

        #endregion

    }
}
